#include "selfinfowidget.h"

/**********************************************************************************************
 * 点击个人头像，所弹出的个人信息窗口
 **********************************************************************************************/
using namespace model;

SelfInfoWidget::SelfInfoWidget(QWidget *parent)
    :QDialog(parent)
{
    //1.设置整个窗口的属性
    this->setFixedSize(500, 250);
    this->setWindowTitle("个人信息");
    this->setWindowIcon(QIcon(":/resource/selfInfo.png"));

    // 窗口被关闭时, 自动销毁这个对话框对象.
    this->setAttribute(Qt::WA_DeleteOnClose);
    // 把窗口移动到鼠标当前的位置
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    glayout = new QGridLayout();
    // layout->setSpacing(0);
    glayout->setHorizontalSpacing(10);
    glayout->setVerticalSpacing(6);
    glayout->setContentsMargins(20, 20, 20, 0);
    glayout->setAlignment(Qt::AlignTop);
    this->setLayout(glayout);

     // 3. 创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75, 75);
    avatarBtn->setIconSize(QSize(75, 75));
    avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    glayout->addWidget(avatarBtn, 0, 0, 3, 1);

    QString labelStyle = "QLabel { font-size: 14px; font-weight: 800; }";
    QString btnStyle = "QPushButton { border: none; background-color: transparent; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";
    QString editStyle = "QLineEdit { border: none; border-radius:5px; padding-left:2px; }";

    int height = 30;

    // 4. 添加用户的 id 的显示
    idTag = new QLabel();
    idTag->setFixedSize(50, height);
    idTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    idTag->setText("序号");
    idTag->setStyleSheet(labelStyle);

    idLabel = new QLabel();
    idLabel->setFixedHeight(height);
    idLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);


    // 5. 添加用户的名字的显示
    nameTag = new QLabel();
    nameTag->setFixedSize(50, height);
    nameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    nameTag->setText("昵称");
    nameTag->setStyleSheet(labelStyle);

    nameLabel = new QLabel();
    nameLabel->setFixedHeight(height);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    nameModifyBtn = new QPushButton();
    nameModifyBtn->setFixedSize(70, 25);
    nameModifyBtn->setIconSize(QSize(20, 20));
    nameModifyBtn->setIcon(QIcon(":/resource/edit.png"));
    nameModifyBtn->setStyleSheet(btnStyle);

    nameEdit = new QLineEdit();
    nameEdit->setFixedHeight(height);
    nameEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameEdit->setStyleSheet(editStyle);
    nameEdit->hide();

    nameSubmitBtn = new QPushButton();
    nameSubmitBtn->setFixedSize(70, 25);
    nameSubmitBtn->setIconSize(QSize(20, 20));
    nameSubmitBtn->setIcon(QIcon(":/resource/commit.png"));
    nameSubmitBtn->setStyleSheet(btnStyle);
    nameSubmitBtn->hide();

    // 6. 添加个性签名
    descTag = new QLabel();
    descTag->setFixedSize(50, height);
    descTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    descTag->setText("签名");
    descTag->setStyleSheet(labelStyle);

    descLabel = new QLabel();
    descLabel->setFixedHeight(height);
    descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    descModifyBtn = new QPushButton();
    descModifyBtn->setFixedSize(70, 25);
    descModifyBtn->setIconSize(QSize(20, 20));
    descModifyBtn->setIcon(QIcon(":/resource/edit.png"));
    descModifyBtn->setStyleSheet(btnStyle);

    descEdit = new QLineEdit();
    descEdit->setFixedHeight(height);
    descEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    descEdit->setStyleSheet(editStyle);
    descEdit->hide();

    descSubmitBtn = new QPushButton();
    descSubmitBtn->setFixedSize(70, 25);
    descSubmitBtn->setIconSize(QSize(20, 20));
    descSubmitBtn->setIcon(QIcon(":/resource/commit.png"));
    descSubmitBtn->setStyleSheet(btnStyle);
    descSubmitBtn->hide();

    // 7. 添加电话
    phoneTag = new QLabel();
    phoneTag->setFixedSize(50, height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(labelStyle);

    phoneLabel = new QLabel();
    phoneLabel->setFixedHeight(height);
    phoneLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    phoneModifyBtn = new QPushButton();
    phoneModifyBtn->setFixedSize(70, 25);
    phoneModifyBtn->setIconSize(QSize(20, 20));
    phoneModifyBtn->setIcon(QIcon(":/resource/edit.png"));
    phoneModifyBtn->setStyleSheet(btnStyle);

    phoneEdit = new QLineEdit();
    phoneEdit->setFixedHeight(height);
    phoneEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    phoneEdit->setStyleSheet(editStyle);
    phoneEdit->hide();

    phoneSubmitBtn = new QPushButton();
    phoneSubmitBtn->setFixedSize(70, 25);
    phoneSubmitBtn->setIconSize(QSize(20, 20));
    phoneSubmitBtn->setIcon(QIcon(":/resource/commit.png"));
    phoneSubmitBtn->setStyleSheet(btnStyle);
    phoneSubmitBtn->hide();


    // 8. 添加验证码(默认隐藏)
    verifyCodeTag = new QLabel();
    verifyCodeTag->setFixedSize(50, height);
    verifyCodeTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    verifyCodeTag->setText("验证码");
    verifyCodeTag->setStyleSheet(labelStyle);
    verifyCodeTag->hide();

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setFixedHeight(height);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    verifyCodeEdit->setStyleSheet(editStyle);
    verifyCodeEdit->hide();

    getVerifyCodeBtn = new QPushButton();
    getVerifyCodeBtn->setText("获取验证码");
    getVerifyCodeBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { background-color: rgb(231, 231, 231); }");
    getVerifyCodeBtn->setFixedSize(70, height);
    getVerifyCodeBtn->hide();

    // 9. 添加到布局管理器. 第 0 列被头像占用了. 下列内容都是从第一列开始往后排
    glayout->addWidget(idTag, 0, 1);
    glayout->addWidget(idLabel, 0, 2);

    glayout->addWidget(nameTag, 1, 1);
    glayout->addWidget(nameLabel, 1, 2);
    glayout->addWidget(nameModifyBtn, 1, 3);

    glayout->addWidget(descTag, 2, 1);
    glayout->addWidget(descLabel, 2, 2);
    glayout->addWidget(descModifyBtn, 2, 3);

    glayout->addWidget(phoneTag, 3, 1);
    glayout->addWidget(phoneLabel, 3, 2);
    glayout->addWidget(phoneModifyBtn, 3, 3);

#ifdef TEST_UI
    idLabel->setText("1234");
    nameLabel->setText("张三");
    descLabel->setText("从今天开始认真敲代码");
    phoneLabel->setText("13712345678");
    avatarBtn->setIcon(QIcon(":/resource/lion.png"));
#endif

    //10.连接信号槽
    initSignalSlots();

    //11.加载数据到页面
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* myself = dataCenter->getMyself();
    if(myself != nullptr)
    {
        avatarBtn->setIcon(myself->avatar);
        idLabel->setText(myself->userId);
        nameLabel->setText(myself->nickName);
        descLabel->setText(myself->description);
        phoneLabel->setText(myself->phoneNumber);
    }

}

void SelfInfoWidget::initSignalSlots()
{
    // a.主界面，连接点击头像触发
    // b.个人详情页面，连接点击编辑触发
    connect(nameModifyBtn,&QPushButton::clicked,this,[=](){
        //将当前的nameLabel、nameModifyBtn隐藏
        nameLabel->hide();
        nameModifyBtn->hide();
        glayout->removeWidget(nameLabel);
        glayout->removeWidget(nameModifyBtn);
        //把nameEdit、nameSubmit显示出来
        nameEdit->show();
        nameSubmitBtn->show();
        glayout->addWidget(nameEdit,1,2);
        glayout->addWidget(nameSubmitBtn,1,3);
        //把输入框中的内容设置过来
        nameEdit->setText(nameLabel->text());
    });

    connect(descModifyBtn, &QPushButton::clicked, this, [=]() {
        descLabel->hide();
        descModifyBtn->hide();
        glayout->removeWidget(descLabel);
        glayout->removeWidget(descModifyBtn);

        descEdit->show();
        descSubmitBtn->show();
        glayout->addWidget(descEdit, 2, 2);
        glayout->addWidget(descSubmitBtn, 2, 3);

        descEdit->setText(descLabel->text());
    });


    connect(phoneModifyBtn, &QPushButton::clicked, this, [=]() {
        phoneLabel->hide();
        phoneModifyBtn->hide();
        glayout->removeWidget(phoneLabel);
        glayout->removeWidget(phoneModifyBtn);

        phoneEdit->show();
        phoneSubmitBtn->show();
        glayout->addWidget(phoneEdit, 3, 2);
        glayout->addWidget(phoneSubmitBtn, 3, 3);

        verifyCodeTag->show();
        verifyCodeEdit->show();
        getVerifyCodeBtn->show();
        glayout->addWidget(verifyCodeTag, 4, 1);
        glayout->addWidget(verifyCodeEdit, 4, 2);
        glayout->addWidget(getVerifyCodeBtn, 4, 3);

        phoneEdit->setText(phoneLabel->text());
    });

    //修改昵称
    connect(nameSubmitBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickSubmitNameBtn);

    //修改签名
    connect(descSubmitBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickSubmitDescBtn);

    //获取验证码
    connect(getVerifyCodeBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickGetVerifyCodeBtn);

    //提交修改手机号
    connect(phoneSubmitBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickSubmitPhoneBtn);
    //修改个人头像
    connect(avatarBtn,&QPushButton::clicked,this,&SelfInfoWidget::clickAvatarBtn);
}

void SelfInfoWidget::clickSubmitNameBtn()
{
    //1.获取名称
    const QString& nickName = nameEdit->text();
    if(nickName.isEmpty())
    {
        LOG() << "名称不能为空";
        return;
    }
    //2.发送网络请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::changeNickNameDone,this,&SelfInfoWidget::clickSubmitNameBtnDone,Qt::UniqueConnection);
    dataCenter->ChangeNickNameAsync(nickName);
}

void SelfInfoWidget::clickSubmitNameBtnDone()
{
    //对界面控件进行替换
    //1.文本框
    glayout->removeWidget(nameEdit);
    nameEdit->hide();
    glayout->addWidget(nameLabel,1,2);
    nameLabel->show();
    nameLabel->setText(nameEdit->text());

    //2.按钮
    glayout->removeWidget(nameSubmitBtn);
    nameSubmitBtn->hide();
    glayout->addWidget(nameModifyBtn,1,3);
    nameModifyBtn->show();
}

void SelfInfoWidget::clickSubmitDescBtn()
{
    //1.获取签名
    const QString& desc = descEdit->text();
    if(desc.isEmpty())
    {
        LOG() << "签名不能为空";
        return;
    }
    //2.发送网络请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::changeDescDone,this,&SelfInfoWidget::clickSubmitDescDone,Qt::UniqueConnection);
    dataCenter->ChangeDescAscyc(desc);

}

void SelfInfoWidget::clickSubmitDescDone()
{
    //1.文本框
    glayout->removeWidget(descEdit);
    descEdit->hide();
    glayout->addWidget(descLabel,2,2);
    descLabel->show();
    descLabel->setText(descEdit->text());

    //2.按钮
    glayout->removeWidget(descSubmitBtn);
    descSubmitBtn->hide();
    glayout->addWidget(descModifyBtn,2,3);
    descModifyBtn->show();
}

void SelfInfoWidget::clickGetVerifyCodeBtn()
{
    //1.获取手机号
    const QString& phone = phoneEdit->text();
    if(phone.isEmpty())
    {
        LOG() << "手机号不能为空";
        return;
    }
    //1.给服务器发送请求
    DataCenter* dataCenter = DataCenter::getInstance();
    //接收服务器的处理完毕信号
    connect(dataCenter,&DataCenter::getVerifyCodeDone,this,[](){
        //不需要做其它处理，只需要提示一下
        Toast::showMessage("验证码发送成功");
    });
    dataCenter->getVerifycodeAsync(phone);

    //2.保存当前输入的手机号，防止提交时用户又在输入框中修改了
    this->phoneToChange = phone;

    //3.将发送验证码按钮禁用
    this->getVerifyCodeBtn->setEnabled(false);
    leftTime = 30;
    QTimer* timer = new QTimer(this);
    //倒计时结束处理
    connect(timer,&QTimer::timeout,this,[=](){
        if(leftTime <= 1)
        {
            getVerifyCodeBtn->setEnabled(true);
            getVerifyCodeBtn->setText("获取验证码");
            timer->stop();
            timer->deleteLater();
            return;
        }

        --leftTime;
        getVerifyCodeBtn->setText(QString::number(leftTime) + "s");
    });
    timer->start(1000);
}

void SelfInfoWidget::clickSubmitPhoneBtn()
{
    //3.获取到用户输入的验证码
    const QString verifyCode = verifyCodeEdit->text();
    if(verifyCode.isEmpty())
    {
        Toast::showMessage("验证码不能为空");
        return;
    }

    //1.先判定验证码是否已经收到
    DataCenter* dataCenter = DataCenter::getInstance();
    QString verifyCodeId = dataCenter->getVerigyCodeId();
    if(verifyCodeId.isEmpty())
    {
        Toast::showMessage("服务器尚未返回响应，请稍后重试！");
        return;
    }

    //2.收到了验证码Id，将其清空
    dataCenter->resetVerifyCodeId("");
    verifyCodeEdit->setText("");
    //4.发送网络请求
    connect(dataCenter,&DataCenter::changePhoneDone,this,&SelfInfoWidget::clickPhoneSubmitBtnDone);
    dataCenter->changePhoneAsync(this->phoneToChange,verifyCodeId,verifyCode);

    //停止倒计时
    leftTime = 1;
}

void SelfInfoWidget::clickPhoneSubmitBtnDone()
{
    glayout->removeWidget(verifyCodeEdit);
    glayout->removeWidget(verifyCodeTag);
    glayout->removeWidget(getVerifyCodeBtn);
    glayout->removeWidget(phoneEdit);
    glayout->removeWidget(phoneSubmitBtn);

    verifyCodeEdit->hide();
    verifyCodeTag->hide();
    getVerifyCodeBtn->hide();
    phoneEdit->hide();
    phoneSubmitBtn->hide();

    glayout->addWidget(phoneLabel,3,2);
    phoneLabel->show();
    phoneLabel->setText(this->phoneToChange);
    glayout->addWidget(phoneModifyBtn,3,3);
    phoneModifyBtn->show();
}

void SelfInfoWidget::clickAvatarBtn()
{
    //1.弹出对话框，选择文件
    QString filter = "Image Files (*.png *.jpg *.jpeg)";

    QString imagePath = QFileDialog::getOpenFileName(this,"选择头像",QDir::homePath(),filter);
    if(imagePath.isEmpty())
    {
        LOG() << "用户未选择任何头像";
        return;
    }
    //2.根据路径，加载头像内容
    QByteArray imageBytes = loadFile2ByteArray(imagePath);

    //3.发送修改头像的请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::changeAvatarDone,this,&SelfInfoWidget::clickAvatarBtnDone);
    dataCenter->changeAvatarAsync(imageBytes);
}

void SelfInfoWidget::clickAvatarBtnDone()
{
    DataCenter* dataCenter = DataCenter::getInstance();
    UserInfo* myself = dataCenter->getMyself();
    avatarBtn->setIcon(myself->avatar);
}
